#include <QPainter>
#include "handlerpath.h"
#include <QObject>
#include <QGraphicsEllipseItem>
#include <QGraphicsLineItem>
#include "Aria.h"
#include "ArNetworking.h"

handlerPath::handlerPath(QObject *parent,ArClientBase *client) :
    QObject(parent),
    getPathCB(this,&handlerPath::getPath)
    //getPathStatusCB(this,&handlerPath::getPathStatus)
{
    handledClient = client;

    if(!handledClient->dataExists("getPath"));
    else{
        pathpoints.begin();
        pathlines.begin();
        handledClient->addHandler("getPath", &getPathCB);
        //handledClient->addHandler("getPath", &getPathStatusCB);
        handledClient->request("getPath",500);
    }
}


void handlerPath::getPath(ArNetPacket *packet){

    int npoints;
    int X,Y;
    QGraphicsLineItem *line;
    QPoint *point;
    QPen qpen(Qt::blue);


    while(pathlines.size()){
        emit signal_remove_line(pathlines.back());
        pathlines.pop_back();
    }

    npoints = packet->bufToByte2();

    pathpoints.resize(npoints);

    if(!npoints) return;




    for(int x=0;x<npoints;x++){

        X = packet->bufToByte4();
        Y = packet->bufToByte4();



        point = new QPoint(X,Y*(-1));


        pathpoints[x] = point;
    }

    pathlines.resize(npoints-1);

    for(int x=1;x<npoints;x++){

        line = new QGraphicsLineItem(pathpoints[x-1]->x(),pathpoints[x-1]->y(),pathpoints[x]->x(),pathpoints[x]->y(),0,0);



        qpen.setWidth(50);

        line->setPen(qpen);

        pathlines[x-1] = line;

        emit signal_paint_line(line);
    }

}

/*void handlerPath::getPathStatus(ArNetPacket *packet){



}*/
